// echarts图表数据接口 
var express = require('express');
var router = express.Router();
const {sequelize,User} = require('../../models')
const {Op} = require('sequelize')
const {NotFoundError,success,failure} = require('../../utils/response')

// 此为饼形图展示用户男女比例图表数据
router.get('/sex',async function (req,res) {
    try {
        const male = await User.count({where:{sex:0}})
        const female = await User.count({where:{sex:1}})
        const unknown = await User.count({where:{sex:2}})
        const data = [
            {value:male,name:'男性'},
            {value:female,name:'女性'},
            {value:unknown,name:'未填写'},
        ]
        success(res,'查询用户性别成功',data)
    } catch (error) {
        failure(res,error)
    }
})

// 此为折线图展示用户注册日期的数据
router.get('/user',async function (req,res) {
    try {
        const [results] = await sequelize.query("SELECT DATE_FORMAT(`createdAt`,'%m-%d') AS `day`,COUNT(*) AS `value` FROM `Users` GROUP BY `day` ORDER BY `day` ASC")
        const data = {
            days:[],
            values:[]
        }
        results.forEach(item => {
            data.days.push(item.day)
            data.values.push(item.value)
        });
        success(res,'查询用户性别成功',data)
    } catch (error) {
        failure(res,error)
    }
})

module.exports = router